/**
 * 
 */
package mocku.helper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * Take care of reflection-based invocations and stuff. 
 *
 * @author aambertin
 */
public class ReflectionHelper {

	/**
	 * Invoke :method on :target using :args .
	 * @param target
	 * @param method
	 * @param args
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static Object invoke (Object target, Method method, Object[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		return method.invoke(target, args);
	}
	
	/**
	 * return method corresponding to signature :name :paramTypes on :instance.class
	 * @param instance
	 * @param name
	 * @param paramTypes
	 * @return
	 */
	public static Method getMethod(Object instance, String name, Class[] paramTypes)  {
		try {
			return instance.getClass().getMethod(name, paramTypes);
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * Create an instance of :clazzFQN using :params.
	 * @param clazzFQN
	 * @param params
	 * @return
	 */
	public static Object createInstance(Class clazz, Class[] types, Object[] params) {
		try {
			return clazz.getConstructor(types).newInstance(params);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("Error invoking mocked constructor.",e);
		}
	}
}
